package com.springframework;

import com.self.springframework.aop.ClassFilter;
import com.self.springframework.aop.TargetSource;
import com.self.springframework.aop.aspectj.AspectJExpressionPointcut;
import com.self.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor;
import com.self.springframework.aop.AdvisedSupport;
import com.self.springframework.aop.framework.ProxyFactory;
import com.self.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor;
import com.self.springframework.context.support.ClassPathXmlApplicationContext;
import com.springframework.aop.IUserService;
import com.springframework.aop.UserService;
import com.springframework.aop.UserServiceBeforeAdvice;
import com.springframework.aop.UserServiceInterceptor;
import org.aopalliance.intercept.MethodInterceptor;
import org.junit.Before;
import org.junit.Test;

public class ApiTest2 {
    private AdvisedSupport advisedSupport;

    @Before
    public void init() {
        // 目标对象
        com.springframework.aop.UserService userService = new com.springframework.aop.UserService();
        // 组装代理信息
        advisedSupport = new AdvisedSupport();
        advisedSupport.setTargetSource(new TargetSource(userService));
        advisedSupport.setMethodInterceptor(new UserServiceInterceptor());
        advisedSupport.setMethodMatcher(new AspectJExpressionPointcut("execution(* com.springframework.aop.IUserService.*(..))"));
    }

    @Test
    public void proxyFactory_test() {
        advisedSupport.setProxyTargetClass(true);// false/true:JDK动态代理/CGlib动态代理
        IUserService proxy = (IUserService) new ProxyFactory(advisedSupport).getProxy();
        System.out.println(proxy.queryUserInfo());
    }

    @Test
    public void beforeAdvice_test() {
        UserServiceBeforeAdvice beforeAdvice = new UserServiceBeforeAdvice();
        MethodBeforeAdviceInterceptor interceptor = new MethodBeforeAdviceInterceptor(beforeAdvice);
        advisedSupport.setMethodInterceptor(interceptor);
        IUserService proxy = (IUserService) new ProxyFactory(advisedSupport).getProxy();
        System.out.println(proxy.queryUserInfo());
    }

    @Test
    public void advisor_test() {
        // 目标对象
        UserService userService = new UserService();
        AspectJExpressionPointcutAdvisor advisor = new AspectJExpressionPointcutAdvisor();
        advisor.setExpression("execution(* com.springframework.aop.IUserService.*(..))");
        advisor.setAdvice(new MethodBeforeAdviceInterceptor(new UserServiceBeforeAdvice()));

        ClassFilter classFilter = advisor.getPointcut().getClassFilter();
        if (classFilter.matches(userService.getClass())) {
            AdvisedSupport advisedSupport = new AdvisedSupport();
            TargetSource targetSource = new TargetSource(userService);
            advisedSupport.setTargetSource(targetSource);
            advisedSupport.setMethodMatcher(advisor.getPointcut().getMethodMatcher());
            advisedSupport.setMethodInterceptor((MethodInterceptor) advisor.getAdvice());

            IUserService proxy = (IUserService) new ProxyFactory(advisedSupport).getProxy();
            System.out.println(proxy.queryUserInfo());
        }
    }

    @Test
    public void aop_test() {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring-aop2.xml");
        IUserService userService = applicationContext.getBean("userService", IUserService.class);
        System.out.println(userService.queryUserInfo());
    }


}
